In [3]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
In [3]:
np.random.seed(1000)
y=np.random.standard_normal(20)
In [4]:
y
Out[4]:
In [5]:
x=range(len(y))
In [6]:
x
Out[6]:
In [7]:
plt.plot(x,y)
plt.show()
In [8]:
plt.plot(y) #接收数组数据
plt.show()
In [9]:
plt.plot(y.cumsum())
plt.show()
In [10]:
plt.plot(y.cumsum())
plt.grid(True)
plt.axis('tight')
Out[10]:
In [11]:
plt.plot(y.cumsum())
plt.grid(True)
plt.xlim(-1,20)
plt.ylim(np.min(y.cumsum())-1,np.max(y.cumsum())+1)
Out[11]:
In [12]:
plt.figure(figsize=(7,4))
plt.plot(y.cumsum(),'b',lw=1.5)
plt.plot(y.cumsum(),'ro')
plt.grid(True)
plt.axis('tight')
plt.xlabel('Index')
plt.xlabel('value')
plt.title('A smple plot')
Out[12]:
In [13]:
np.random.seed(2000)
y=np.random.standard_normal((20,2)).cumsum(axis=0)
y
Out[13]:
In [14]:
plt.figure(figsize=(7,4))
plt.plot(y,lw=1.5)
plt.plot(y,'ro')
plt.grid(True)
plt.axis('tight')
plt.xlabel('Index')
plt.xlabel('value')
plt.title('A smple plot')
Out[14]:
In [15]:
plt.figure(figsize=(7,4))
plt.plot(y[:,0],lw=1.5,label='1st')
plt.plot(y[:,1],lw=1.5,label='2nd')
plt.plot(y,'ro')
plt.grid(True)
plt.legend(loc=0)
plt.axis('tight')
plt.xlabel('Index')
plt.xlabel('value')
plt.title('A smple plot')
Out[15]:
In [16]:
y[:,0]=y[:,0]*100
plt.figure(figsize=(7,4))
plt.plot(y[:,0],lw=1.5,label='1st')
plt.plot(y[:,1],lw=1.5,label='2nd')
plt.plot(y,'ro')
plt.grid(True)
plt.legend(loc=0)
plt.axis('tight')
plt.xlabel('Index')
plt.xlabel('value')
plt.title('A smple plot')
Out[16]:
In [17]:
# 使用双轴图
fig,ax1=plt.subplots()
plt.plot(y[:,0],'b',lw=1.5,label='1st')
plt.plot(y[:,0],'ro')
plt.grid(True)
plt.legend(loc=0)
plt.axis('tight')
plt.xlabel('Index')
plt.ylabel('value 1st')
plt.title('A smple plot')
ax2=ax1.twinx()
plt.plot(y[:,1],'g',lw=1.5,label='2nd')
plt.plot(y[:,1],'ro')
plt.legend(loc=0)
plt.ylabel('value 2nd')
Out[17]:
In [18]:
#画两张子图
plt.figure(figsize=(7,5))
plt.subplot(211)
plt.plot(y[:,0],'b',lw=1.5,label='1st')
plt.plot(y[:,0],'ro')
plt.grid(True)
plt.legend(loc=0)
plt.axis('tight')
plt.xlabel('Index')
plt.ylabel('value 1st')
plt.title('A smple plot')
plt.subplot(212)
plt.plot(y[:,1],'g',lw=1.5,label='2nd')
plt.plot(y[:,1],'ro')
plt.grid(True)
plt.legend(loc=0)
plt.axis('tight')
plt.xlabel('Index')
plt.ylabel('value 2nd')
plt.title('A smple plot')
Out[18]:
In [19]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
plt.figure(figsize=(9,4))
plt.subplot(121)
plt.plot(y[:,0],'b',lw=1.5,label='1st')
plt.plot(y[:,0],'ro')
plt.grid(True)
plt.legend(loc=0)
plt.axis('tight')
plt.xlabel('Index')
plt.ylabel('value 1st')
plt.title('1st data set')
plt.subplot(122)
plt.bar(np.arange(len(y)),y[:,1],width=0.5,color='g',label='2nd')
plt.grid(True)
plt.legend(loc=0)
plt.axis('tight')
plt.xlabel('Index')
plt.ylabel('value 2nd')
plt.title('2nd data set')
Out[19]:
In [20]:
y=np.random.standard_normal((1000,2))
plt.figure(figsize=(7,5))
plt.plot(y[:,0],y[:,1],'ro')
plt.grid(True)
plt.xlabel('1st')
plt.ylabel('2nd')
plt.title('Scatter Plot')
Out[20]:
In [21]:
y=np.random.standard_normal((1000,2))
plt.figure(figsize=(20,10))
plt.plot(y[:,0],y[:,1],'ro',marker='o')
plt.grid(True)
plt.xlabel('1st')
plt.ylabel('2nd')
plt.title('Scatter Plot')
Out[21]:
In [22]:
#加入权重
c=np.random.randint(0,10,len(y))
plt.figure(figsize=(7,5))
plt.scatter(y[:,0],y[:,1],c=c)
plt.colorbar()
plt.grid(True)
plt.xlabel('1st')
plt.ylabel('2nd')
plt.title('Scatter Plot')
Out[22]:
In [23]:
plt.figure(figsize=(7,4))
plt.hist(y,label=['1st','2nd'],bins=25)
plt.grid(True)
plt.xlabel('value')
plt.ylabel('frequency')
plt.title('Histgram')
Out[23]:
In [24]:
plt.figure(figsize=(7,4))
plt.hist(y,label=['1st','2nd'],bins=25,cumulative=True)
plt.grid(True)
plt.xlabel('value')
plt.ylabel('frequency')
plt.title('Histgram')
Out[24]:
In [25]:
plt.figure(figsize=(7,4))
plt.hist(y,label=['1st','2nd'],color=['b','g'],stacked=True,bins=25,edgecolor="black")
plt.grid(True)
plt.legend(loc=0)
plt.xlabel('value')
plt.ylabel('frequency')
plt.title('Histgram')
Out[25]:
In [26]:
fig,ax=plt.subplots(figsize=(7,4))
plt.boxplot(y)
plt.grid(True)
plt.setp(ax,xticklabels=['1st','2nd'])
plt.xlabel('data set')
plt.ylabel('value')
plt.title('boxplot')
Out[26]:
In [27]:
plt.figure(figsize=(20,100))
line=plt.plot(y,'g')
plt.setp(line,linestyle='-')
Out[27]:
In [48]:
def func(x):
return 0.5*np.exp(x)+1
a,b=0.5,1.5
x=np.linspace(0,2)
y=func(x)
fig,ax=plt.subplots(figsize=(7,5))
plt.plot(x,y,'b',linewidth=2)
plt.ylim(ymin=0)
Out[48]:
In [60]:
Ix=np.linspace(a,b)
Iy=func(Ix)
verts=[(a,0)]+list(zip(Ix,Iy))+[(b,0)]
verts
q=[list(zip(Ix,Iy))]
type(q)
verts.count((0.5,0))
Out[60]:
In [65]:
from matplotlib.patches import Polygon
poly=Polygon(verts,facecolor='0.7',edgecolor='0.5')
fig,ax=plt.subplots(figsize=(7,5))
plt.plot(x,y,'b',linewidth=2)
plt.ylim(ymin=0)
ax.add_patch(poly)
Out[65]:
In [69]:
from matplotlib.patches import Polygon
poly=Polygon(verts,facecolor='0.7',edgecolor='0.5')
fig,ax=plt.subplots(figsize=(7,5))
plt.plot(x,y,'b',linewidth=2)
plt.ylim(ymin=0)
ax.add_patch(poly)
plt.text(0.5*(a+b),1,r"$\int_a^b f(x)\mathrm{d}x$",horizontalalignment='center',fontsize=20)
plt.figtext(0.9,0.075,'$x$')
plt.figtext(0.075,0.9,'$f(x)$')
ax.set_xticks((a,b))
ax.set_xticklabels(('$a$','$b$'))
ax.set_yticks([func(a),func(b)])
ax.set_yticklabels(('$f(a)$','$f(b)$'))
plt.grid(True)
In [125]:
import mpl_finance as mpf
import tushare as ts
start=(2014,5,1)
end=(2014,6,30)
wdyx = ts.get_k_data('000651','2019-08-01')
In [126]:
wdyx.info()
In [127]:
wdyx[:3]
Out[127]:
In [128]:
from matplotlib.pylab import date2num
import datetime
In [129]:
def date_to_num(dates):
num_time = []
for date in dates:
date_time = datetime.datetime.strptime(date,'%Y-%m-%d')
num_date = date2num(date_time)
num_time.append(num_date)
return num_time
In [130]:
# dataframe轉換為二維陣列
mat_wdyx = wdyx.values
In [131]:
mat_wdyx
Out[131]:
In [132]:
num_time = date_to_num(mat_wdyx[:,0])
In [133]:
num_time
Out[133]:
In [134]:
mat_wdyx[:,0] = num_time
In [135]:
mat_wdyx[:20]
Out[135]:
In [146]:
fig,ax=plt.subplots(figsize=(8,5))
fig.subplots_adjust(bottom=0.2)
# mpf.candlestick_ohlc(ax,mat_wdyx[:20],width=0.6,colorup='r',colordown='g')
mpf.candlestick_ochl(ax,mat_wdyx[:20],width=0.2,colorup='r',colordown='g',alpha=1.0)
plt.grid(True)
ax.xaxis_date() #设置x轴日期
ax.autoscale_view() #自动调整一下视图
plt.xticks(rotation=30)
Out[146]:
存在问题:mpf的蜡烛图生成有问题?¶
改用candlestick_ochl解决了问题
In [150]:
fig,ax=plt.subplots(figsize=(8,5))
fig.subplots_adjust(bottom=0.2)
mpf.plot_day_summary_oclh(ax,mat_wdyx[:20],colorup='b',colordown='r')
plt.grid(True)
ax.xaxis_date() #设置x轴日期
ax.autoscale_view() #自动调整一下视图
plt.xticks(rotation=30)
Out[150]:
In [151]:
mat_wdyx
Out[151]:
In [162]:
fig,(ax1,ax2)=plt.subplots(2,sharex=True,figsize=(8,5))
mpf.candlestick_ochl(ax1,mat_wdyx,width=0.6,colorup='b',colordown='r')
ax1.set_title('geli')
ax1.set_ylabel('price')
ax1.grid(True)
ax1.xaxis_date()
plt.bar(mat_wdyx[:,0]-0.25,mat_wdyx[:,5],width=0.5)
ax2.set_ylabel('volume')
ax2.grid(True)
ax2.autoscale_view()
plt.xticks(rotation=30)
Out[162]:
In [5]:
#绘制三维图
strike=np.linspace(50,150,24)
ttm=np.linspace(0.5,2.5,24)
strike,ttm=np.meshgrid(strike,ttm)
strike[:2]
iv=(strike-100)**2/(100*strike)/ttm
from mpl_toolkits.mplot3d import Axes3D
fig=plt.figure(figsize=(9,6))
ax=fig.gca(projection='3d')
surf=ax.plot_surface(strike,ttm,iv,rstride=2,cstride=2,cmap=plt.cm.coolwarm,linewidth=0.5,antialiased=True)
ax.set_xlabel("strike")
ax.set_ylabel("time-to-maturity")
ax.set_zlabel("implied volatility")
fig.colorbar(surf,shrink=0.5,aspect=5)
Out[5]:
In [9]:
#三维散点图
fig=plt.figure(figsize=(8,5))
ax=fig.add_subplot(111,projection='3d')
ax.view_init(30,60)
ax.scatter(strike,ttm,iv,zdir='z',s=25,c='b',marker='^')
ax.set_xlabel('strike')
ax.set_ylabel("time-to-maturity")
ax.set_zlabel("implied volatility")
Out[9]:
In [ ]: